﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using FarseerPhysics;
using FarseerPhysics.Dynamics;
using FarseerPhysics.Collision;
using FarseerPhysics.Factories;
using FarseerPhysics.Common;
using FarseerPhysics.Common.Decomposition;
using FarseerPhysics.Common.ConvexHull;
using EpicBall.ScreenSystem;
using EpicBall.Camera;
using EpicBall.Levels.Classes;
using EpicBall.Levels.Classes.Enemies;

namespace EpicBall.Levels
{
    class TestLevel: PhysicsGameScreen
    {
        int screenWidth;
        int screenHeight;

        Texture2D collisionTexture;
        Texture2D ballTexture;
        Texture2D blankTexture;
        Texture2D waveTexture;
        Texture2D playerTopArrowTexture;

        Texture2D collectableTexture;
        Texture2D startTexture;
        Texture2D endTexture;

        Texture2D backgroundTexture;


        public TestLevel() : base(2000 , 1200) { }

        public override void LoadContent()
        {
            base.LoadContent();
            screenWidth = (int)GameStack.Dimensions.X;
            screenHeight = (int)GameStack.Dimensions.Y;

            collisionTexture = GameStack.Content.Load<Texture2D>("test_level");
            ballTexture = GameStack.Content.Load<Texture2D>("character/ball1/face0");
            blankTexture = GameStack.Content.Load<Texture2D>("blank");
            waveTexture = GameStack.Content.Load<Texture2D>("ObstacleTextures/World/Sand/wave1");
            playerTopArrowTexture = GameStack.Content.Load<Texture2D>("character/player_top_arrow");
            collectableTexture = GameStack.Content.Load<Texture2D>("ObstacleTextures/collectable");
            startTexture = GameStack.Content.Load<Texture2D>("ObstacleTextures/warpIn");
            endTexture = GameStack.Content.Load<Texture2D>("ObstacleTextures/warpOut");

            backgroundTexture = GameStack.Content.Load<Texture2D>("backgrounds/desert");

            _airResistance = new Vector2(2.5f, 0);
            _speedThreshold = 12.5f;

            gameWorld.Gravity = Vector2.UnitY * 10;
            FixtureFactory.AttachCircle(ConvertUnits.ToSimUnits(12.5), 15, player);

            /*LevelObject ground = new LevelObject(this, screenWidth, 16);
            ground.Color = Color.Green;
            Fixture groundFixture = FixtureFactory.AttachRectangle(ConvertUnits.ToSimUnits(screenWidth), ConvertUnits.ToSimUnits(16), 10, Vector2.Zero, ground.Body);
            SlideCollision(ground.Body);*/

            Collectable collectable = new Collectable(this, collectableTexture);
            AddObjectToWorld(collectable, new Vector2(600, 200),0, 2, 3);

            LevelObject start = new Warp(this, true, startTexture);
            AddObjectToWorld(start, new Vector2(100, 100), 0, 1, 0, true);

            LevelObject end = new Warp(this, false, endTexture);
            AddObjectToWorld(end, new Vector2(1000, 600), 0, 1, 10, false);

            LevelObject waveBody = PhysicsGameScreen.ImageToNewObjectPolygon(this, waveTexture, gameWorld, 1f, 1f);
            AddObjectToWorld(waveBody, new Vector2(600, 400), 1);
            
            SpikeTrap spiketrap = new SpikeTrap(this);
            AddObjectToWorld(spiketrap, new Vector2(150,300),0);

            StaticTurret turret = new StaticTurret(this,0.5f,false,true,2f);
            AddObjectToWorld(turret, new Vector2(800, 300), 1);

            StaticTurret peaceful_turret = new StaticTurret(this,3);
            AddObjectToWorld(peaceful_turret,new Vector2(1000, 300), 1);

            Door door = new Door(this,1);
            AddObjectToWorld(door, new Vector2(500, 300), 0);

            Key key = new Key(this, doors);
            AddObjectToWorld(key, new Vector2(300, 300), 0);

            MovingPlatform platform = new MovingPlatform(this, GameStack.Content.Load<Texture2D>("Enemies/projectilePlatform"));
            platform.AddPathNode(new MovingPlatform.AIPathNode(new Vector2(300,200)));
            platform.AddPathNode(new MovingPlatform.AIPathNode(new Vector2(350, 400)));
            platform.AddPathNode(new MovingPlatform.AIPathNode(new Vector2(500, 400)));
            platform.Ready();

            AddObjectToWorld(platform, new Vector2(300, 200), 1);

            //Setup positions
           
            _startPos = start.Body.Position;
            player.Position = _startPos;

            //ground.Body.Position = ConvertUnits.ToSimUnits(new Vector2(screenWidth / 2, 500));  
        }

        

        public override void Draw(GameTime gameTime)
        {
            GameStack.SpriteBatch.Begin(0, null, null, null, null, null, _camera.View);
            GameStack.SpriteBatch.Draw(backgroundTexture, new Rectangle(0,0, this.Width, this.Height), null, Color.White, 0.0f, Vector2.Zero, SpriteEffects.None, 1f);
            GameStack.SpriteBatch.End();
            base.Draw(gameTime);
        }

        public override void Update(GameTime gameTime)
        {
            base.Update(gameTime);
            if (isDead)
            {
                isDead = false;
                LoadContent();
            }
        }
    }
}
